using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VFPACK
    {
        public DataRow Convert2FPACK(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryFPACK(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["FPMANDANT"] = " ".PadRight(2 ); 
         dwReturn["FPLIEFSCH"] = " ".PadRight(8 ); 
         dwReturn["FPPACKNR"] = " ".PadRight(4 ); 
         dwReturn["FPPOS"] = " ".PadRight(3 ); 
         dwReturn["FPAUFTYP"] = " ".PadRight(1 ); 
         dwReturn["FPAUFKRS"] = " ".PadRight(2 ); 
         dwReturn["FPAUFLFD"] = " ".PadRight(6 ); 
         dwReturn["FPAUFPOS"] = " ".PadRight(3 ); 
         dwReturn["FPTEILNR"] = " ".PadRight(4 ); 
         dwReturn["FPART"] = " ".PadRight(20 ); 
         dwReturn["FPVAR"] = " ".PadRight(2 ); 
         dwReturn["FPFARBE"] = " ".PadRight(10 ); 
         dwReturn["FPGROE"] = " ".PadRight(7 ); 
         dwReturn["FPAUFM"] = " ".PadRight(2 ); 
         dwReturn["FPVPEINH"] = " ".PadRight(3 ); 
         dwReturn["FPANZVP"] = " ".PadRight(0 ); 
         dwReturn["FPZS"] = " ".PadRight(2 ); 
         dwReturn["FPCHAR"] = " ".PadRight(0 ); 
         dwReturn["FPGNR"] = " ".PadRight(0 ); 
         dwReturn["FPGNRPOS"] = " ".PadRight(0 ); 
         dwReturn["FPLO"] = " ".PadRight(2 ); 
         dwReturn["FPLB"] = " ".PadRight(2 ); 
         dwReturn["FPLP"] = " ".PadRight(8 ); 
         dwReturn["FPAUFBKZ"] = " ".PadRight(1 ); 
         dwReturn["FPMENGE"] = " ".PadRight(8 ); 
         dwReturn["FPCHARL"] = " ".PadRight(15 ); 
         dwReturn["FPCHARLZUS"] = " ".PadRight(15 ); 
         dwReturn["FPNETKG"] = " ".PadRight(9 ); 
         dwReturn["FPFILLER"] = " ".PadRight(37 ); 
         dwReturn["FPAENZL"] = " ".PadRight(0 ); 
         dwReturn["FPAENB"] = " ".PadRight(8 ); 
         dwReturn["FPAEND"] = " ".PadRight(0 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapFPACK(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveFPACK(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryFPACK(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapFPACK(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertFPACK(dwLocal, ITXDBVERSION);
            }
            UpdateFPACK(dwLocal);
        }
        public void InsertFPACK(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO FPACK(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryFPACK(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM FPACK WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM FPACK  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "FPACK";
            Return = Result;
            return Return;
        }
        public void DeleteFPACK(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM FPACK   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateFPACK(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE FPACK SET "
                    +"FPMANDANT= :FPMANDANT,"
                    +"FPLIEFSCH= :FPLIEFSCH,"
                    +"FPPACKNR= :FPPACKNR,"
                    +"FPPOS= :FPPOS,"
                    +"FPAUFTYP= :FPAUFTYP,"
                    +"FPAUFKRS= :FPAUFKRS,"
                    +"FPAUFLFD= :FPAUFLFD,"
                    +"FPAUFPOS= :FPAUFPOS,"
                    +"FPTEILNR= :FPTEILNR,"
                    +"FPART= :FPART,"
                    +"FPVAR= :FPVAR,"
                    +"FPFARBE= :FPFARBE,"
                    +"FPGROE= :FPGROE,"
                    +"FPAUFM= :FPAUFM,"
                    +"FPVPEINH= :FPVPEINH,"
                    +"FPANZVP= :FPANZVP,"
                    +"FPZS= :FPZS,"
                    +"FPCHAR= :FPCHAR,"
                    +"FPGNR= :FPGNR,"
                    +"FPGNRPOS= :FPGNRPOS,"
                    +"FPLO= :FPLO,"
                    +"FPLB= :FPLB,"
                    +"FPLP= :FPLP,"
                    +"FPAUFBKZ= :FPAUFBKZ,"
                    +"FPMENGE= :FPMENGE,"
                    +"FPCHARL= :FPCHARL,"
                    +"FPCHARLZUS= :FPCHARLZUS,"
                    +"FPNETKG= :FPNETKG,"
                    +"FPFILLER= :FPFILLER,"
                    +"FPAENZL= :FPAENZL,"
                    +"FPAENB= :FPAENB,"
                    +"FPAEND= :FPAEND,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("FPMANDANT", OracleType.NVarChar).Value = dwData["FPMANDANT"].ToString(); 
          or1.Parameters.Add("FPLIEFSCH", OracleType.NVarChar).Value = dwData["FPLIEFSCH"].ToString(); 
          or1.Parameters.Add("FPPACKNR", OracleType.NVarChar).Value = dwData["FPPACKNR"].ToString(); 
          or1.Parameters.Add("FPPOS", OracleType.NVarChar).Value = dwData["FPPOS"].ToString(); 
          or1.Parameters.Add("FPAUFTYP", OracleType.NVarChar).Value = dwData["FPAUFTYP"].ToString(); 
          or1.Parameters.Add("FPAUFKRS", OracleType.NVarChar).Value = dwData["FPAUFKRS"].ToString(); 
          or1.Parameters.Add("FPAUFLFD", OracleType.NVarChar).Value = dwData["FPAUFLFD"].ToString(); 
          or1.Parameters.Add("FPAUFPOS", OracleType.NVarChar).Value = dwData["FPAUFPOS"].ToString(); 
          or1.Parameters.Add("FPTEILNR", OracleType.NVarChar).Value = dwData["FPTEILNR"].ToString(); 
          or1.Parameters.Add("FPART", OracleType.NVarChar).Value = dwData["FPART"].ToString(); 
          or1.Parameters.Add("FPVAR", OracleType.NVarChar).Value = dwData["FPVAR"].ToString(); 
          or1.Parameters.Add("FPFARBE", OracleType.NVarChar).Value = dwData["FPFARBE"].ToString(); 
          or1.Parameters.Add("FPGROE", OracleType.NVarChar).Value = dwData["FPGROE"].ToString(); 
          or1.Parameters.Add("FPAUFM", OracleType.NVarChar).Value = dwData["FPAUFM"].ToString(); 
          or1.Parameters.Add("FPVPEINH", OracleType.NVarChar).Value = dwData["FPVPEINH"].ToString(); 
          or1.Parameters.Add("FPANZVP", OracleType.NVarChar).Value = dwData["FPANZVP"].ToString(); 
          or1.Parameters.Add("FPZS", OracleType.NVarChar).Value = dwData["FPZS"].ToString(); 
          or1.Parameters.Add("FPCHAR", OracleType.NVarChar).Value = dwData["FPCHAR"].ToString(); 
          or1.Parameters.Add("FPGNR", OracleType.NVarChar).Value = dwData["FPGNR"].ToString(); 
          or1.Parameters.Add("FPGNRPOS", OracleType.NVarChar).Value = dwData["FPGNRPOS"].ToString(); 
          or1.Parameters.Add("FPLO", OracleType.NVarChar).Value = dwData["FPLO"].ToString(); 
          or1.Parameters.Add("FPLB", OracleType.NVarChar).Value = dwData["FPLB"].ToString(); 
          or1.Parameters.Add("FPLP", OracleType.NVarChar).Value = dwData["FPLP"].ToString(); 
          or1.Parameters.Add("FPAUFBKZ", OracleType.NVarChar).Value = dwData["FPAUFBKZ"].ToString(); 
          or1.Parameters.Add("FPMENGE", OracleType.NVarChar).Value = dwData["FPMENGE"].ToString(); 
          or1.Parameters.Add("FPCHARL", OracleType.NVarChar).Value = dwData["FPCHARL"].ToString(); 
          or1.Parameters.Add("FPCHARLZUS", OracleType.NVarChar).Value = dwData["FPCHARLZUS"].ToString(); 
          or1.Parameters.Add("FPNETKG", OracleType.NVarChar).Value = dwData["FPNETKG"].ToString(); 
          or1.Parameters.Add("FPFILLER", OracleType.NVarChar).Value = dwData["FPFILLER"].ToString(); 
          or1.Parameters.Add("FPAENZL", OracleType.NVarChar).Value = dwData["FPAENZL"].ToString(); 
          or1.Parameters.Add("FPAENB", OracleType.NVarChar).Value = dwData["FPAENB"].ToString(); 
          or1.Parameters.Add("FPAEND", OracleType.NVarChar).Value = dwData["FPAEND"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
